package io.sage.modules.flow.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.sage.modules.flow.service.FlowViewEventService;
import io.sage.modules.flow.service.ReadsenseApiService;
import io.sage.modules.readsenseapi.entity.BFlowCustomerGroups;
import io.sage.modules.readsenseapi.entity.BFlowViewEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("flowViewEventService")
public class FlowViewEventServiceImpl implements FlowViewEventService {
    @Autowired
    private ReadsenseApiService readsenseApiService;

    /**
     * 查看客流总量
     * @param params
     * @return
     */
    @Override
    public List<BFlowViewEvent> selectFlowViewEventList(Map<String, Object> params) {
        /*?shop_id=&from_date=1565193600&to_date=1565279941*/
        String uri="company/view_events";
        Map<String,String> json=readsenseApiService.gets(uri);
        //转化为数组
        JSONArray jsonArray = JSONArray.parseArray(json.get("entity"));
        //转化为集合
        List<BFlowViewEvent> bFlowViewEvents = jsonArray.toJavaList(BFlowViewEvent.class);
        return bFlowViewEvents;
    }

    /**
     * 批量删除,url地址错误
     * @param ids
     * @return
     */
    @Override
    public int deleteBatchIds(String[] ids) {
        String id="";
        for (int i = 0; i < ids.length; i++) {
            id=ids[i];
            String uri="company/events/"+id;
            System.out.println("id==>"+id);
            String getsagin = readsenseApiService.deletes(uri);
        }
        return 1;
    }

    /**
     * 查看详情,url地址错误
     * @param id
     * @return
     */
    @Override
    public BFlowViewEvent selectByPrimaryKey(String id) {
        String uri="company/events/"+id;
        String json = readsenseApiService.getsagin(uri);
        //jaon字符串转换为实体类
        BFlowViewEvent bFlowViewEvent = JSONObject.toJavaObject(JSONObject.parseObject(json), BFlowViewEvent.class);
        return bFlowViewEvent;
    }

    /**
     * 通过id寻找,url地址错误
     * @param bFlowViewEvent
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(BFlowViewEvent bFlowViewEvent) {
        Map<String,Object> map=new HashMap<>();
        String uri="company/view_events";
//        map.put("customer_group",customerGroupsVo);
        String posts = readsenseApiService.posts(uri, map);
        return 1;
    }
    /*
    *添加,url地址错误
     */
    @Override
    public int insert(BFlowViewEvent bFlowViewEvent) {
        Map<String,Object> map=new HashMap<>();
        String uri="company/events";
//        map.put("customer_group",customerGroupsVo);
        String posts = readsenseApiService.posts(uri, map);
        return 1;
    }
}
